<template>
  <div class='questions-choice-container'>
    <el-card>
        <questions-pagetools/>
      <questions-form @search="hSearch"/>
      <el-tabs  type="card" @tab-click="handleClick">
        <el-tab-pane label="全部"></el-tab-pane>
        <el-tab-pane label="待审核" ></el-tab-pane>
        <el-tab-pane label="已审核" ></el-tab-pane>
        <el-tab-pane label="已拒绝" ></el-tab-pane>
      </el-tabs>
      <el-alert
      :title="alertText"
      type="info"
      class="alert"
      :closable="false"
      show-icon
      ></el-alert>
       <el-table
      :header-cell-style="{background:'#fafafa'}"
      :data="choiceList"
      style="width: 100%">
        <el-table-column
          prop="number"
          label="试题编号"
          width="120">
        </el-table-column>
        <el-table-column
          prop="subject"
          label="学科"
          width="120">

        </el-table-column>
        <el-table-column
          prop="catalogID"
          label="目录"
          width="120">

        </el-table-column>
        <el-table-column

          label="题型"
          width="120">
          <template #default="{row}">
           {{formatQuestionType(row.questionType)}}
          </template>
        </el-table-column>
        <el-table-column
          label="题干"
          width="280">
          <template #default="{row}" >
            <span v-html="row.question"></span>
          </template>
        </el-table-column>
        <el-table-column
          label="录入时间"
          width="180"
          >
          <template #default="{row}">
           {{formatDate(row.addDate)}}
          </template>
        </el-table-column>
        <el-table-column

          label="难度"
          width="80">
          <template #default="{row}">
           {{formatDifficulty(row.difficulty)}}
          </template>
        </el-table-column>
        <el-table-column
          prop="creator"
          label="录入人"
          width="120">
        </el-table-column>
        <el-table-column

          label="审核状态"
          width="120">
           <template #default="{row}">
           {{formatchkState(row.chkState+1)}}
          </template>
        </el-table-column>
        <el-table-column
          prop="chkRemarks"
          label="审核意见"
          width="150">
        </el-table-column>
        <el-table-column
          prop="chkUser"
          label="审核人"
          width="120">
        </el-table-column>
        <el-table-column
          label="发布状态"
          width="120">
          <template #default="{row}">
           {{row.chkState===1&&row.publishState==0?formatpublishState(row.publishState+3):formatpublishState(row.publishState+1)}}
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          fixed="right"
          width="220"
          align="center"
          >
          <template #default="{row}">
            <el-button size="small" type="text" @click="hPreview(row)" >预览</el-button>
            <el-button size="small" type="text" @click="hCheck(row)"  :disabled="row.chkState!==0">审核</el-button>
            <el-button size="small" type="text" @click="$router.push(`/questions/new?id=${row.id}`)" :disabled="row.publishState===1">修改</el-button>
            <el-button size="small" type="text" @click="hPublish(row)" :disabled="row.chkState!==1">{{row.publishState?'下架':'上架' }}</el-button>
            <el-button size="small" type="text" @click="hDel(row.id)" :disabled="row.publishState===1">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-row type="flex" justify="end" class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          background
          @current-change="handleCurrentChange"
          :current-page="q.page"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="q.pagesize"
          layout="prev,pager,next,sizes,jumper"
          :total="total">
        </el-pagination>
      </el-row>
    </el-card>
    <el-dialog title="题目预览" :visible.sync="showDialog" width="900px">
      <questions-preview :curId="curId" v-if="showDialog"></questions-preview>
        <span slot="footer" class="dialog-footer" >
          <div class="close">
            <el-button @click="showDialog = false"  type="primary">关闭</el-button>
          </div>
        </span>
   </el-dialog>
   <el-dialog title="题目审核" :visible.sync="showCheckDialog" width="400px">
     <el-form>
      <el-form-item >
        <el-radio-group v-model="checkForm.chkState">
          <el-radio :label="1">通过</el-radio>
          <el-radio :label="2">拒绝</el-radio>
        </el-radio-group>
      </el-form-item>
       <el-form-item>
        <el-input type="textarea" placeholder="请输入审核意见" v-model.trim="checkForm.chkRemarks"></el-input>
      </el-form-item>
     </el-form>
     <div slot="footer" class="dialog-footer">
       <div class="close">
      <el-button @click="showCheckDialog = false">取消</el-button>
      <el-button type="primary" @click="hSubmit">确认</el-button>
      </div>
    </div>
   </el-dialog>
  </div>
</template>

<script>
import * as directorys from '@/api/hmmm/directorys.js'
import * as questions from '@/api/hmmm/questions.js'
import { difficulty, questionType, publishType, chkType } from '@/api/hmmm/constants.js'
import moment from 'moment'
const difficultyMnus = {}
difficulty.forEach(item => {
  difficultyMnus[item.value] = item.label
})
const questionTypeMnus = {}
questionType.forEach(item => {
  questionTypeMnus[item.value] = item.label
})
const chkStateMnus = {}
chkType.forEach(item => {
  chkStateMnus[item.value] = item.label
})
const publishStateMnus = {}
publishType.forEach(item => {
  publishStateMnus[item.value] = item.label
})
export default {
  created () {
    this.loadChoiceList()
  },
  methods: {
    async hPublish (row) {
      const str = row.publishState ? '下架' : '上架'
      this.$confirm(`确认${str}该题目?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        if (row.publishState) {
          row.publishState = 0
        } else {
          row.publishState = 1
        }
        const res = await questions.choicePublish({ id: row.id, publishState: row.publishState })
        if (res.status === 200) {
          this.loadChoiceList()
          this.$message({
            type: 'success',
            message: `${str}成功!`
          })
          this.loadChoiceList()
        }
      }).catch((error) => {
        this.$message.error(error)
      })
    },
    hCheck (row) {
      this.curId = row.id
      this.showCheckDialog = true
    },
    async hSubmit () {
      if (this.checkForm.chkRemarks === '') return this.$message.warning('请输入审核意见')
      this.checkForm.id = this.curId
      const res = await questions.choiceCheck(this.checkForm)
      console.log(res)
      this.loadChoiceList()
      this.showCheckDialog = false
    },
    hPreview (row) {
      this.curId = row.id
      this.showDialog = true
    },
    hDel (id) {
      this.$confirm('确认删除该题目?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await questions.remove({ id })
        if (res.status === 200) {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.loadChoiceList()
        }
      }).catch((error) => {
        this.$message.error(error)
      })
    },
    async  hSearch (form) {
      console.log(form)
      this.q = { ...this.q, ...form }
      Object.keys(this.q).forEach(key => {
        if (this.q[key] === '') {
          delete this.q[key]
        }
      })
      await this.loadChoiceList()
      Object.keys(this.q).forEach(key => {
        if (key === 'page' || key === 'pagesize') {

        } else {
          delete this.q[key]
        }
      })
    },
    handleClick (tab, event) {
      console.log(tab.index - 1)
      this.chkState = tab.index - 1
      if (this.chkState !== -1) {
        this.q = { ...this.q, chkState: this.chkState }
      } else {
        delete this.q.chkState
      }
      this.loadChoiceList()
    },
    async loadChoiceList () {
      try {
        const res = await questions.choice(this.q)
        if (res.status !== 200) return this.$message.error('获取失败')
        // this.$message.success('获取基础题库列表成功')
        const catalogList = await directorys.list({ page: 1, pagesize: 100 })
        // console.log(catalogList)
        const directory = {}
        catalogList.data.items.forEach(item => {
          const { id, directoryName } = item
          directory[id] = directoryName
        })
        // console.log(directory)
        this.choiceList = res.data.items
        this.choiceList.forEach(item => {
          item.catalogID = directory[item.catalogID]
        })
        console.log(this.choiceList, ' this.choiceList')
        this.total = res.data.counts
        this.alertText = `共${res.data.counts}条数据`
      } catch (error) {
        console.log(error)
      }
    },
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      this.q.pagesize = val
      this.q.page = 1
      this.loadChoiceList()
    },
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.q.page = val
      this.loadChoiceList()
    },
    formatDate (date) {
      return moment(date).format('YYYY-MM-DD HH:mm:ss')
    },
    formatDifficulty (id) {
      return difficultyMnus[id] || ''
    },
    formatQuestionType (id) {
      return questionTypeMnus[id] || ''
    },
    formatchkState (id) {
      return chkStateMnus[id] || ''
    },
    formatpublishState (id) {
      return publishStateMnus[id] || ''
    }

  },
  data () {
    return {
      chkState: -1,
      alertText: '共10条数据',
      q: {
        page: 1,
        pagesize: 10
      },
      total: 1,
      choiceList: [],
      curId: '',
      showDialog: false,
      showCheckDialog: false,
      checkForm: {
        chkState: 1,
        chkRemarks: ''
      }
    }
  }
}
</script>

<style scoped lang='less'>
.questions-choice-container{
  padding: 0 10px;
    margin: 10px 0;
  .alert {
  margin-bottom:15px ;
  }
  .pagination{
    margin: 20px 0;
  }
  .close{
    display: flex;
    justify-content: flex-end;
  }
}
</style>
